/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.plechinger.mcremote.client;

import com.plechinger.mcremote.data.Request;
import com.plechinger.mcremote.data.RequestMethod;
import com.plechinger.mcremote.data.Response;
import java.io.*;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.zip.GZIPOutputStream;

/**
 *
 * @author Lukas
 */
public class SimpleClient {

    public static Object sendAndReceiveObjects(int port, String hostname, Object request) throws UnknownHostException, IOException, ClassNotFoundException {
        InetAddress addr = InetAddress.getByName(hostname);
        Socket socket = new Socket(addr, port);
        OutputStream os = socket.getOutputStream();
        os=new BufferedOutputStream(os);
        os=new GZIPOutputStream(os);
        ObjectOutputStream oos = new ObjectOutputStream(os);
        InputStream is = socket.getInputStream();
        is=new BufferedInputStream(is);
        ObjectInputStream ois = new ObjectInputStream(is);

        oos.writeObject(request);
        oos.flush();
        Object answer = ois.readObject();
        System.out.println(answer.toString());
        return answer;
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        try {
            Request request = new Request();
            request.setMethod(RequestMethod.START);
            Object answer = SimpleClient.sendAndReceiveObjects(29900, "localhost", request);
            Response res = (Response)answer;
            System.out.println(res.getStatus());
            System.out.println(res.getMessage());
            System.out.println(res.getResponseObject());
            System.out.println(answer);
        } catch (Exception ex) {
            System.out.println(ex.toString());
        }
    }
}
